<template>
  <div class="add-role">
    <header class="add-header">
      <div class="left">
        <span class="arrow" @click="$router.back()"><i class="el-icon-arrow-left" />返回</span>
        <span>|</span>
        <span>添加角色</span>
      </div>
      <div class="right">
        黑马程序员
      </div>
    </header>
    <main class="add-main">
      <div class="step-container">
        <el-steps direction="vertical" :active="step">
          <el-step title="角色信息" />
          <el-step title="权限信息" />
          <el-step title="检查并完成" />
        </el-steps>
      </div>
      <div v-show="step===1" class="form-container">
        <div class="title">角色信息</div>
        <div class="form">
          <el-form ref="roleForm" class="form-box" :model="roleForm" :rules="roleRules">
            <el-form-item label="角色名称" prop="roleName">
              <el-input v-model="roleForm.roleName" />
            </el-form-item>
            <el-form-item label="角色描述">
              <el-input v-model="roleForm.remark" />
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div v-show="step===2" class="form-container">
        <div class="title">权限配置</div>
        <div class="form">
          <div class="tree-wrapper">
            <div v-for="item in treeList" :key="item.id" class="tree-item">
              <div class="tree-title">{{ item.title }}</div>
              <el-tree
                ref="tree"
                :data="item.children"
                show-checkbox
                default-expand-all
                check-strictly
                node-key="id"
                highlight-current
                :props="{ label: 'title' }"
              />
            </div>
          </div>
        </div>
      </div>
      <div v-show="step===3" class="form-container">
        <div class="title">检查并完成</div>
        <div class="form">
          <div class="info">
            <div class="form-item">角色名称：{{ roleForm.roleName }}</div>
            <div class="form-item">角色描述：{{ roleForm.remark }}</div>
            <div class="form-item">角色权限：</div>
            <div class="tree-wrapper">
              <div v-for="item in treeList" :key="item.id" class="tree-item">
                <div class="tree-title">{{ item.title }}</div>
                <el-tree
                  ref="diabledTree"
                  :data="item.children"
                  show-checkbox
                  check-strictly
                  default-expand-all
                  node-key="id"
                  :highlight-current="false"
                  :props="{ label: 'title',disabled:()=>true}"
                />
              </div>
            </div>
          </div>
        </div>
      </div>
    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button v-if="step>1" @click="prevStep">上一步</el-button>
        <el-button v-if="step<3" type="primary" @click="nextStep">下一步</el-button>
        <el-button v-if="step===3" type="primary" @click="addRole">确认添加</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { getTreeListAPI, createRoleUserAPI } from '@/api/system'
export default {
  data() {
    return {
      step: 1,
      roleForm: {
        roleName: '',
        remark: ''
      },
      roleRules: {
        roleName: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ]
      },
      treeList: []
    }
  },
  created() {
    this.getTreeList()
  },
  methods: {
    prevStep() {
      if (this.step === 1) return
      this.step--
    },
    nextStep() {
      if (this.step === 3) return
      if (this.step === 1) {
        // 等表单校验通过后再下一步
        this.$refs.roleForm.validate(valid => {
          if (valid) {
            this.step++
          }
        })
      } else if (this.step === 2) {
        const checkedArr = []
        // 权限校验(获取每一个树形组件勾选的节点，有一个勾选了就算通过)
        this.$refs.tree.forEach(tree => {
          // 获取选中的节点(map是因为后续接口包括设置节点都只需要数字，不需要对象)
          const checkedNodes = tree.getCheckedNodes().map(item => item.id)
          // 将获取到选中的节点塞入准备的数据
          checkedArr.push(checkedNodes)
        })
        // checkedArr存放了所有树勾选的所有节点
        // 数组的flat方法可以将二维数组扁平化成一维数组
        console.log(checkedArr.flat())
        // flag为true  表示所有的权限都是空的
        // const flag = checkedArr.every(item => item.length === 0)
        // flag为true  表示有勾选的权限
        // const flag = checkedArr.some(item => item.length > 0)
        this.roleForm.perms = checkedArr
        if (this.roleForm.perms.flat().length > 0) {
          this.step++
          this.$refs.diabledTree.forEach((tree, index) => {
            // this.roleForm.perms是数组对象，elementui需要的是数组id
            tree.setCheckedKeys(this.roleForm.perms[index])
          })
          // 早上的禁用是给数据的每一个树形添加了disabled的属性
          // 因为当前页面有两个地方用到了数据，但是一个是禁用，一个不是禁用
        } else {
          this.$message.error('至少勾选一个权限')
        }
      }
    },
    async getTreeList() {
      const res = await getTreeListAPI()
      this.treeList = res.data
    },
    async addRole() {
      const res = await createRoleUserAPI(this.roleForm)
      if (res.code === 10000) {
        this.$message.success('添加成功')
        this.$router.back()
      } else {
        this.$message.error('新增失败,' + res.msg)
      }
    }
  }
}
</script>
<style scoped lang="scss">
.add-role {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    justify-content: space-between;
    padding: 0 20px;
    height: 64px;
    line-height: 64px;
    font-size: 16px;
    font-weight: bold;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }

      .arrow {
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    position: fixed;
    top: 64px;
    bottom: 88px;
    width: 100%;
    overflow-y: auto;
    background: #f4f6f8;
    padding: 20px 230px;
    display: flex;
    .step-container {
      height: 400px;
      width: 200px;
    }
    .form-container {
      flex:1;
      background-color: #fff;
      overflow-y: auto;
      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }
      .form {
        margin-bottom: 20px;
        padding: 20px 65px 24px;
        .el-form {
          display: flex;
          flex-wrap: wrap;
        }
        .info{
          font-size:14px;
          color:#666;
          .form-item{
            margin-bottom:20px;
          }
        }
      }
      .form-box{
        width: 600px;
        display: flex;
        flex-direction: column;
      }
    }
  }

  .add-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 24px 50px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;
    text-align: center;
    z-index: 10001;
  }
  .tree-wrapper {
      display: flex;
      justify-content: space-between;
      flex-wrap: wrap;
      .tree-item {
        flex: 1;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }
}
</style>
